//
//  GenderSettingVC.swift
//  WeFamily
//
//  Created by 崔世强 on 2020/7/7.
//  Copyright © 2020 Meak Tsui. All rights reserved.
//

import UIKit

class GenderSettingVC: UITableViewController {
    
    @IBOutlet weak var maleCell: UITableViewCell!
    @IBOutlet weak var femaleCell: UITableViewCell!
    
    private var selectedGender:Gender = .unknown
    
    lazy var userService = UserService()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let Defaults = UserDefaults(suiteName: USER_DEFAULTS_NAME)!
        guard let gender = Defaults[.userGender] else {
            return
        }
        if gender == "1" {
            maleCell.accessoryType = .checkmark
        } else if  gender == "2"{
            femaleCell.accessoryType = .checkmark
        }
        
//        switch MTUser.me().gender {
//        case .male:
//            maleCell.accessoryType = .checkmark
//        case .female:
//            femaleCell.accessoryType = .checkmark
//        default:
//            maleCell.accessoryType = .none
//            femaleCell.accessoryType = .none
//        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            selectedGender = .male
            maleCell.accessoryType = .checkmark
            femaleCell.accessoryType = .none
        } else {
            selectedGender = .female
            maleCell.accessoryType = .none
            femaleCell.accessoryType = .checkmark
        }
    }
    
    
    @IBAction func donBtnClick(_ sender: Any) {
        let gender = selectedGender.rawValue
        
        self.view.makeToastActivity(.center)
        let uid = AppStatusManager.instance.userLoginStatus.1 ?? 0
        userService.updateMyInfo(uid, gender: gender) { success, msg in
            DispatchQueue.main.async {
                self.view.hideModalToastActivity()
                if (success) {
                    self.navigationController?.popViewController(animated: true)
                } else {
                    self.view.makeToast("修改失败", duration: 3.0, position: .center)
                }
            }
        }
    }
}
